<?php
require 'jtools.inc';
require 'mysql_connect.php';
echo '
<script type="text/javascript">	
  $(function() {
	$(\'tr.parentr\')
		.css("cursor","pointer")
		.attr("title","Click to expand/collapse")
		.click(function(){
			$(this).siblings(\'.child-\'+this.id).toggle();
		});
	$(\'tr[class^=child-]\').hide().children(\'td\');
	});

</script>
<table class="detail" id="detail_table">
	<colgroup><col style="width:140px;">
		<col style="width:182px;">
		<col style="width:214px;">
		<col style="width:120px;">
		<col style="width:120px;">
		<col style="width:120px;">
		<col style="width:120px;">
	</colgroup>
<tbody><tr>
		<th>&nbsp;</th>
		<th>&nbsp;</th>
		<th>iPhone</th>
		<th>iPad</th>
		<th>Android</th>
		<th>Mobile</th>
		<th>Total</th>
	</tr>';
	
switch  ($_REQUEST["content_for"]) {
	case 'to_buy':
		$vListingType = 'For Sale';
		break;
	case 'to_rent':
		$vListingType = 'To Rent';
		break;
	case 'to_oh':
		$vListingType = 'Open House';
		break;
}

$vQuery = 'SELECT SearchType, sum(iPhone) AS AlliPhone, sum(iPad) AS AlliPad, sum(Android) AS AllAndroid, sum(Mobile) AS AllMobile, sum(iPhone) 
+ sum(iPad) + sum(Android) + sum(Mobile) AS Total FROM SearchMetrics WHERE ListingType="' . $vListingType .'" GROUP BY SearchType';
$rsSearchTypes = mysql_query($vQuery) or die ('Invalid Query 1: ' . mysql_error());
$vCounter=0;
while ($rsSearchType=mysql_fetch_array($rsSearchTypes,MYSQL_BOTH)) {
	$vCounter = $vCounter + 100;
	echo '<tr id="row' . $vCounter . '" class="parentr" style="cursor: pointer; " title="Click to expand/collapse">
			<td class="ftd">' . $rsSearchType["SearchType"] . ' <span>V</span></td>
			<td>Show All</td>
			<td>' . $rsSearchType["AlliPhone"] . '</td>
			<td>' . $rsSearchType["AlliPad"] . '</td>
			<td>' . $rsSearchType["AllAndroid"] . '</td>
			<td>' . $rsSearchType["AllMobile"] . '</td>
			<td>' . $rsSearchType["Total"] . '</td>
		</tr>';
	$vQuery = "SELECT Category, iPhone, iPad, Android, Mobile, iPhone + iPad + Android + Mobile AS Total FROM SearchMetrics WHERE ListingType='" . $vListingType ."' AND SearchType='" . $rsSearchType["SearchType"] . "' ORDER BY SortOrder";
	$rsRows = mysql_query($vQuery) or die ('Invalid Query 12: ' . mysql_error());
	while ($rsRow=mysql_fetch_array($rsRows,MYSQL_BOTH)) {
		echo '<tr class="child-row' . $vCounter . '" style="display: none; ">
				<td>&nbsp;</td>
				<td ALIGN="LEFT">' . $rsRow["Category"] . '</td>
				<td>' . $rsRow["iPhone"] . '</td>
				<td>' . $rsRow["iPad"] . '</td>
				<td>' . $rsRow["Android"] . '</td>
				<td>' . $rsRow["Mobile"] . '</td>
				<td>' . $rsRow["Total"] . '</td>
			</tr>';
	}
	echo '<tr class="child-row' . $vCounter . '" style="display: none; ">
			<td>&nbsp;</td>
		</tr>';
} 

echo'</tbody></table>';
?>